<?php

namespace app\model\oa;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * 笔记模型
 * @package app\model\oa
 */
class Note extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'note';

    // 主键
    protected $pk = 'id';

    // 自动时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';

    // 字段类型转换
    protected $type = [
        'id' => 'integer',
        'title' => 'string',
        'content' => 'string',
        'user_id' => 'integer',
        'category_id' => 'integer',
        'is_public' => 'integer',
        'is_favorite' => 'integer',
        'color' => 'string',
        'tags' => 'string',
        'attachment' => 'json',
        'create_time' => 'int',
        'update_time' => 'int',
        'delete_time' => 'int',
    ];

    // 笔记颜色常量
    const COLOR_DEFAULT = '#ffffff';  // 默认白色
    const COLOR_RED = '#ffebee';      // 红色
    const COLOR_YELLOW = '#fffde7';   // 黄色
    const COLOR_GREEN = '#e8f5e9';    // 绿色
    const COLOR_BLUE = '#e3f2fd';     // 蓝色
    const COLOR_PURPLE = '#f3e5f5';   // 紫色

    /**
     * 获取颜色列表
     */
    public static function getColorList()
    {
        return [
            self::COLOR_DEFAULT => '默认',
            self::COLOR_RED => '红色',
            self::COLOR_YELLOW => '黄色',
            self::COLOR_GREEN => '绿色',
            self::COLOR_BLUE => '蓝色',
            self::COLOR_PURPLE => '紫色',
        ];
    }

    /**
     * 关联用户
     */
    public function user()
    {
        return $this->belongsTo('app\model\admin\User', 'user_id', 'id');
    }

    /**
     * 关联分类
     */
    public function category()
    {
        return $this->belongsTo('app\model\oa\NoteCategory', 'category_id', 'id');
    }

    /**
     * 获取笔记列表
     */
    public static function getList($params = [])
    {
        $query = self::with(['user', 'category'])->order('update_time', 'desc');

        // 按用户筛选
        if (!empty($params['user_id'])) {
            $query->where('user_id', $params['user_id']);
        }

        // 按分类筛选
        if (!empty($params['category_id'])) {
            $query->where('category_id', $params['category_id']);
        }

        // 按公开状态筛选
        if (isset($params['is_public']) && $params['is_public'] !== '') {
            $query->where('is_public', $params['is_public']);
        }

        // 按收藏状态筛选
        if (isset($params['is_favorite']) && $params['is_favorite'] !== '') {
            $query->where('is_favorite', $params['is_favorite']);
        }

        // 按标题搜索
        if (!empty($params['title'])) {
            $query->where('title', 'like', '%' . $params['title'] . '%');
        }

        // 按内容搜索
        if (!empty($params['content'])) {
            $query->where('content', 'like', '%' . $params['content'] . '%');
        }

        // 按标签搜索
        if (!empty($params['tags'])) {
            $query->where('tags', 'like', '%' . $params['tags'] . '%');
        }

        // 按颜色筛选
        if (!empty($params['color'])) {
            $query->where('color', $params['color']);
        }

        return $query->paginate($params['limit'] ?? 15);
    }

    /**
     * 获取我的笔记
     */
    public static function getMyNotes($userId, $params = [])
    {
        $params['user_id'] = $userId;
        return self::getList($params);
    }

    /**
     * 获取公开笔记
     */
    public static function getPublicNotes($params = [])
    {
        $params['is_public'] = 1;
        return self::getList($params);
    }

    /**
     * 获取收藏的笔记
     */
    public static function getFavoriteNotes($userId, $params = [])
    {
        $params['user_id'] = $userId;
        $params['is_favorite'] = 1;
        return self::getList($params);
    }

    /**
     * 收藏笔记
     */
    public function markAsFavorite()
    {
        $this->is_favorite = 1;
        $this->save();
    }

    /**
     * 取消收藏
     */
    public function unmarkAsFavorite()
    {
        $this->is_favorite = 0;
        $this->save();
    }

    /**
     * 设为公开
     */
    public function makePublic()
    {
        $this->is_public = 1;
        $this->save();
    }

    /**
     * 设为私密
     */
    public function makePrivate()
    {
        $this->is_public = 0;
        $this->save();
    }

    /**
     * 获取标签数组
     */
    public function getTagsArray()
    {
        return $this->tags ? explode(',', $this->tags) : [];
    }

    /**
     * 设置标签
     */
    public function setTagsAttribute($value)
    {
        if (is_array($value)) {
            $this->attributes['tags'] = implode(',', $value);
        } else {
            $this->attributes['tags'] = $value;
        }
    }

    /**
     * 搜索器 - 标题
     */
    public function searchTitleAttr($query, $value)
    {
        $query->where('title', 'like', '%' . $value . '%');
    }

    /**
     * 搜索器 - 内容
     */
    public function searchContentAttr($query, $value)
    {
        $query->where('content', 'like', '%' . $value . '%');
    }

    /**
     * 搜索器 - 用户ID
     */
    public function searchUserIdAttr($query, $value)
    {
        $query->where('user_id', $value);
    }

    /**
     * 搜索器 - 分类ID
     */
    public function searchCategoryIdAttr($query, $value)
    {
        $query->where('category_id', $value);
    }

    /**
     * 搜索器 - 是否公开
     */
    public function searchIsPublicAttr($query, $value)
    {
        $query->where('is_public', $value);
    }

    /**
     * 搜索器 - 是否收藏
     */
    public function searchIsFavoriteAttr($query, $value)
    {
        $query->where('is_favorite', $value);
    }

    /**
     * 搜索器 - 颜色
     */
    public function searchColorAttr($query, $value)
    {
        $query->where('color', $value);
    }

    /**
     * 搜索器 - 标签
     */
    public function searchTagsAttr($query, $value)
    {
        $query->where('tags', 'like', '%' . $value . '%');
    }

    /**
     * 搜索器 - 创建时间范围
     */
    public function searchCreateTimeAttr($query, $value)
    {
        if (is_array($value) && count($value) == 2) {
            $query->whereBetween('create_time', $value);
        }
    }

    /**
     * 搜索器 - 更新时间范围
     */
    public function searchUpdateTimeAttr($query, $value)
    {
        if (is_array($value) && count($value) == 2) {
            $query->whereBetween('update_time', $value);
        }
    }
}